<?php

defined( 'PDNS_AUTH' ) or die( "Unauthorized" );

class zoneModel extends Model{
	/**
	 *  Takes an array and assigns variables from it to this object.
	 *
	 *  @param array
	 */
	public function bind( $data ){
		$this->domain->name = $data['domain']['name'];

		// new zones won't have any existing records
		if( isset( $data['record'] )){
			foreach( $data['record'] as $r ){
				$r['domain_id'] = $this->domain->id;
				$this->records[$r['id']] = (object)$r;
			}
		}
		
		foreach( $data['newrecord'] as $r ){
			if( (! $r['name']) OR isset($r['delete']) )
				continue;

			$r['domain_id'] = $this->domain->id;
			$this->records[] = (object)$r;
		}
	}

	/**
	 *  deletes zone and records from db
	 */
	public function delete(){
		$db = DB::getInstance();
		$name = $this->domain->name;
		$id = $this->domain->id;

		echo "<p>Deleting $name...</p>";

		$n = $db->delete( $id, 'id', 'domains' );
		echo "<p>Deleted $n domains.</p>";

		$n = $db->delete( $id, 'domain_id', 'records' );
		echo "<p>Deleted $n records.</p>";

		echo "<p>Done.</p>";

		Message::status( "Domain $name deleted" );

		$db->log( 'action', $name, "Deleted" ); 
	}

	/**
	 * 
	 * @todo should we detect if a row has changed before updating it?
	 */
	public function store(){
		$db = DB::getInstance();

		if( ! $this->domain->id ){
			// new domain for inserting
			$query = "INSERT domains (name,type) VALUES('{$this->domain->name}', 'NATIVE')";
			$db->query( $query );
			
			// get id for record inserts
			$query = "SELECT id, name FROM domains WHERE name='{$this->domain->name}'";
			$this->domain = $db->fetch_object( $query );
		}
		else{
			// update existing domain
			$query = "UPDATE domains SET name='{$this->domain->name}' WHERE id='{$this->domain->id}'";
			$db->query( $query );
		}

		foreach( $this->records as $r ){
			if( isset($r->delete) && ($r->delete) ){
				// delete record
				$query = "DELETE FROM records WHERE id='{$r->id}'";
				$db->query( $query );
				continue;
			}

			if( ! $r->id ){
				// new record
				$query = "INSERT records (domain_id,name,type,content,ttl,prio) VALUES('{$this->domain->id}', '{$r->name}', '{$r->type}', '{$r->content}', '{$r->ttl}', '{$r->prio}')";
				$db->query( $query );
			}
			else{
				// update existing record
				$query = "UPDATE records SET name='{$r->name}',type='{$r->type}',content='{$r->content}',ttl='{$r->ttl}',prio='{$r->prio}' WHERE id='{$r->id}'";
				$db->query( $query );
			}
		}
	}

	public function load( $domain=null ){
		$db = DB::getInstance();

		if( ! $domain )
			$domain = Request::getDomain('zone_id');

		if( is_numeric( $domain )){
			// Request::getDomain only filters for a domain.  so now we filter for int.
			$domain = (int)$domain;
			$query = "SELECT id, name FROM domains WHERE id='$domain'";
		}
		else
			$query = "SELECT id, name FROM domains WHERE name='$domain'";
	
		$this->domain = $db->fetch_object( $query );

		if( $this->domain === false )
			throw new Exception( "Domain does not exist: $domain" );

		// load records for zone
		$query = "SELECT id, domain_id, name, type, content, ttl, prio FROM records WHERE domain_id={$this->domain->id}";
		$this->records = $db->fetch_objects( $query, 'id' );
	}
}
